package com.mobile.struts.action.tools;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.mobile.model.TreeBasePojo;

/**
 * 在建tree过程中，把Pojo对象中相对应的属性映射为tree的属性<br>
 * id<br>
 * text<br>
 * icon<br>
 * leaf<br>
 * 此类必须设置了clazz属性后才能使用
 * @author huangfeng
 *
 */
public class TreeJsObject {
	/**
	 * 映射到tree id属性中的pojo属性名
	 */
	private String id;
	/**
	 * 映射到tree text属性中的pojo属性名
	 */
	private String text;
	/**
	 * 映射到tree icon属性中的pojo属性名
	 */
	private String icon;
	/**
	 * 映射到tree leaf属性中的pojo属性名<br>
	 * 如果leaf为true则认为此treenode的leaf属性为true<br>
	 * 如果leaf为false则认为此treenode的leaf属性为false<br>
	 */
	private String leaf;
	
	private Class clazz;
	
	private Map<String,PropertyDescriptor> propertyDescriptors;
	public TreeJsObject() {
		super();
		// TODO Auto-generated constructor stub
	}
	public TreeJsObject(String id, String text, String icon, String leaf,
			Class clazz) {
		super();
		this.id = id;
		this.text = text;
		this.icon = icon;
		this.leaf = leaf;
		this.clazz = clazz;
		this.propertyDescriptors = this.getPropertyDescriptors(clazz);
	}

	public Class getClazz() {
		return clazz;
	}
	public void setClazz(Class clazz) {
		this.clazz = clazz;
		this.propertyDescriptors = this.getPropertyDescriptors(clazz);
	}
	public Map<String, PropertyDescriptor> getPropertyDescriptors() {
		return propertyDescriptors;
	}
	public void setPropertyDescriptors(
			Map<String, PropertyDescriptor> propertyDescriptors) {
		this.propertyDescriptors = propertyDescriptors;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getIcon() {
		return icon;
	}
	public void setIcon(String icon) {
		this.icon = icon;
	}
	public String getLeaf() {
		return leaf;
	}
	public void setLeaf(String leaf) {
		this.leaf = leaf;
	}
	/**
	 * 获取clazz类中的id、text、icon、leaf相应的PropertyDescriptor
	 * @param clazz
	 * @return
	 */
	public Map<String,PropertyDescriptor> getPropertyDescriptors(Class clazz){
		Map<String,PropertyDescriptor> map =new HashMap<String,PropertyDescriptor>();
		try {
			map.put("id", new PropertyDescriptor(id,clazz));
			map.put("text", new PropertyDescriptor(text,clazz));
			if(icon!=null){
				map.put("icon", new PropertyDescriptor(icon,clazz));
			}
			if(leaf!=null&&!"true".equals(leaf)&&!"false".equals(leaf)){
				map.put("text", new PropertyDescriptor(getText(),clazz));
			}
			
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 获取clazz类中的id、text、icon、leaf相应的PropertyDescriptor
	 * @param clazz
	 * @return
	 */
	public Map<String,PropertyDescriptor> getPropertyDescriptors(String clazz){
		try {
			return getPropertyDescriptors(Class.forName(clazz));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new HashMap<String,PropertyDescriptor>();
	}
	/**
	 * 读取对象中的属性值
	 * @param name	属性名
	 * @param bean	对象
	 * @return
	 */
	public Object getValue(String name,Object bean){
		if(this.propertyDescriptors!=null){
			try {
				return this.propertyDescriptors.get(name).getReadMethod().invoke(bean);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	/**
	 * 把clazz类的对象转换成TreeBasePojo对象
	 * @param bean
	 * @return
	 */
	public TreeBasePojo getTreeBasePojo(Object bean){
		TreeBasePojo pojo = new TreeBasePojo();
		try {
			pojo.setUniqueCode((this.propertyDescriptors.get("id")
					.getReadMethod().invoke(bean)).toString());
			pojo.setText((this.propertyDescriptors.get("text").getReadMethod()
					.invoke(bean)).toString());
			if (icon != null && this.propertyDescriptors.get("icon") != null) {
				pojo.setIcon((this.propertyDescriptors.get("icon")
						.getReadMethod().invoke(bean)).toString());
			}

			if (leaf != null) {
				if("true".equals(leaf)){
					pojo.setIsLeafNode("1");
				}else if("false".equals(leaf)){
					pojo.setIsLeafNode("0");
				}else if (this.propertyDescriptors.get("leaf") != null){
					String leaf = (this.propertyDescriptors.get("leaf")
							.getReadMethod().invoke(bean)).toString();
					pojo.setIsLeafNode(leaf);
				}
				
			} else {
				pojo.setIsLeafNode("0");
			}
			
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pojo; 
	}
}
